<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' encoding='UTF-8' doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" indent='yes'/>

	<!-- xml filesources -->
	<xsl:variable name="src_head" select="document(root/filehead/@src)"/>
	<xsl:variable name="src_head_ie" select="document(root/filehead_ie/@src)"/>
	<xsl:variable name="src_header" select="document(root/fileheader/@src)"/>
	<xsl:variable name="src_navbar" select="document(root/filenavbar/@src)"/>
	<xsl:variable name="src_footer" select="document(root/filefooter/@src)"/>
	
	<!-- template to load a static content -->
	<xsl:template name="Loader">
		<xsl:param name="src"/>  
		<xsl:copy-of select="$src/root/*[name() != 'namespace']"/>
	</xsl:template>

	<!-- template to load extra data content -->
	<xsl:template name="ExtraDataLoader">
		<xsl:param name="src"/>
		<xsl:copy-of select="$src/*[name() != 'namespace']"/>
	</xsl:template>
	
	<!-- template to load IE Fix content -->
	<xsl:template name="IELoader">
		<xsl:param name="src"/>
		<xsl:comment>[if IE 6]><xsl:value-of select="$src"/>&lt;![endif]</xsl:comment>
	</xsl:template>
	
	<!-- template to dinamically create menu -->
	<!-- Signup Menu -->
	<xsl:template name="NavLoader">
		<xsl:param name="src"/>
		<xsl:variable name="page" select="page"/>
		<!-- login -->
		<xsl:copy-of select="$src/root/linkable/a"/>
		<!-- navbar link list -->
		<ul id="nav_list">
			<xsl:if test="page = 'home'">
				<xsl:copy-of select="$src/root/not_linkable/ul/li[text()='Home']"/>
			</xsl:if>
			<xsl:if test="page != 'home'">
				<xsl:copy-of select="$src/root/linkable/ul/li[a[@accesskey='o']]"/>					
			</xsl:if>
			<xsl:if test="page = 'signup'">
				<xsl:copy-of select="$src/root/not_linkable/ul/li[text()='Sign Up']"/>
			</xsl:if>
			<xsl:if test="page != 'signup'">
				<xsl:copy-of select="$src/root/linkable/ul/li[a[@accesskey='s']]"/>
			</xsl:if>
			<xsl:if test="page = 'help'">
					<xsl:copy-of select="$src/root/not_linkable/ul/li[text()='Help']"/>
			</xsl:if>
			<xsl:if test="page != 'help'">
				<xsl:copy-of select="$src/root/linkable/ul/li[a[@accesskey='h']]"/>
			</xsl:if>
			<xsl:if test="page = 'about'">
				<xsl:copy-of select="$src/root/not_linkable/ul/li/a[text()='About']"/>
			</xsl:if>
			<xsl:if test="page != 'about'">
				<xsl:copy-of select="$src/root/linkable/ul/li[a[@accesskey='a']]"/>
			</xsl:if>
			<xsl:if test="page = 'sitemap'">
				<xsl:copy-of select="$src/root/not_linkable/ul/li/a[text()='Sitemap']"/>
			</xsl:if>
			<xsl:if test="page != 'sitemap'">
				<xsl:copy-of select="$src/root/linkable/ul/li[a[@accesskey='m']]"/>
			</xsl:if>
		</ul>
	</xsl:template>
	
	<!-- App Menu -->
	<xsl:template name="AppNavLoader">
		<xsl:param name="src"/>
		<xsl:variable name="page" select="page"/>
		<xsl:copy-of select="$src/root/linkable/*"/>
	</xsl:template>

	<!-- template to generate main content -->
	<xsl:template name="FeedscribeContent">
		<!-- load extra tabindex params -->
		<xsl:call-template name="ExtraDataLoader">
			<xsl:with-param name="src" select="extradata/tabindex"/>
		</xsl:call-template>
		<!-- main tab page -->
		<div id="tab_wrapper">
			<!-- <p class="site_pos">You are here:  <a href="main_app.html">Your page</a> -&gt; Subscribe feeds</p> -->
			<form method="post" id="subs_form">
				<!-- checkurl if not success or submit_err page -->
				
				<xsl:if test="not(success)">
					<xsl:attribute name="action">../cgi-bin/checkurl.pl</xsl:attribute>
				</xsl:if> 
				<!-- subscribe feed if success page / error on submit -->
				
				<xsl:if test="success">
					<xsl:attribute name="action">../cgi-bin/feedscribe.pl</xsl:attribute>
					<!-- <xsl:attribute name="method">get</xsl:attribute> -->
				</xsl:if> 
				
				<!-- main page -->
				<h1>Subscribe to RSS feeds</h1>

				<fieldset><legend>Automatic RSS Checker</legend>
					<xsl:if test="not(success/err_type)">
						<p>Put here the URL of the page with some RSS feeds or a direct link to a feed</p>
					</xsl:if>
					<!-- if not on success page -->
					<xsl:if test="not(success)">
						<label for="http_input">http://</label><input id="http_input" name="url" type="text">
						<xsl:attribute name="value"><xsl:value-of select="error/url"/></xsl:attribute></input>
						<!-- hidden id value -->
						<input id="id" name="id" type="hidden"><xsl:attribute name="value">
							<!-- idpath on error tag -->
							<xsl:value-of select="error/id"/>
						</xsl:attribute></input>
						<!-- hidden step2 value -->
						<input name="step" type="hidden"><xsl:attribute name="value">
							<!-- step value on error tag -->
							<xsl:value-of select="error/step"/>
						</xsl:attribute></input>
						<input value="Check RSS!" type="submit" id="RSSCheckButton" onclick="CheckRSSUrl.check(document.getElementById('http_input').value); return false;" />
					</xsl:if>
					<!-- errors -->
					<xsl:if test='error/err_type'>
						<p id="error" class="error"><xsl:if test="not(error/feed_name)">The URL you specified does not exist or is unreachable!<br/></xsl:if>
						Error type: <xsl:copy-of select="error/err_type"/></p>
					</xsl:if>
					<!-- feed subscribed (no real error) -->
					<xsl:if test='error/subscribed'>
						<p id="subscribed" class="subscribed"> Congratulation, Feed subscribed successfully</p>
					</xsl:if>
					<!-- critical error: typedata not defined -->
					<xsl:if test='error_empty_data'>
						<p id="error" class="error">ERROR NO TYPEDATA SET</p>
					</xsl:if>
					<!-- success page / feed to subscribe (errors may occurr) -->
					<xsl:if test="success">
						<xsl:if test="not(success/err_type)">
							<p id="des_par"><span>You checked for: </span><xsl:value-of select="success/description"/></p>
								<label for="available_feeds" id="feeds_label">Available feeds: </label><select id="available_feeds" name="link">
								<!-- set feed entries -->
								<xsl:for-each select="success/feedreport">
									<option>
										<!-- feed link -->
										<xsl:attribute name="value"><xsl:value-of select="link"/></xsl:attribute>
										<!-- feed name -->
										<xsl:value-of select="title"/>
									</option>
								</xsl:for-each>
								</select>
						</xsl:if>
						<br/><label for="feed_name">Insert a name for your feed : </label><input id="feed_name" name="feed_name">
						<xsl:attribute name="value"><xsl:value-of select="success/feed_name"/></xsl:attribute></input>
						<!-- hidden id value -->
						<input name="id" type="hidden"><xsl:attribute name="value">
							<!-- idpath on success tag -->
							<xsl:value-of select="success/id"/>
						</xsl:attribute></input><br/>
						<!-- hidden step2 value -->
						<xsl:if test="success/step">
							<input name="step" type="hidden"><xsl:attribute name="value">
								<!-- step value on success tag -->
								<xsl:value-of select="success/step"/>
							</xsl:attribute></input>
						</xsl:if>
						<!-- if on success/err_type save feed link -->
						<xsl:if test="success/err_type">
							<input name="link" type="hidden"><xsl:attribute name="value">
								<!-- idpath on success tag -->
								<xsl:value-of select="success/link"/>
							</xsl:attribute></input>
						</xsl:if>
						<input id="sub_button" value="Subscribe!" type="submit"/>
						<!-- errors -->
						<xsl:if test='success/err_type'>
							<p id="error" class="error">An error occurred : <xsl:copy-of select="success/err_type"/></p>
						</xsl:if>
					</xsl:if>
				</fieldset>
			</form>
			<!-- reset button -->
			<xsl:if test="success">
				<form id="reset_form" method="post" action="../cgi-bin/feedscribe.pl">
				<!-- check if on Sign Up -->
					<xsl:if test="success/step">
						<!-- change action script -->
						<xsl:attribute name="action">../cgi-bin/step2.pl</xsl:attribute>
						<!-- hidden step2 value -->
						<input name="step" type="hidden"><xsl:attribute name="value">
							<!-- step value on error tag -->
							<xsl:value-of select="success/step"/>
						</xsl:attribute></input>
					</xsl:if>
					<input name="id" type="hidden">
						<!-- idpath on success tag -->
						<xsl:attribute name="value">
							<xsl:value-of select="success/id"/>
						</xsl:attribute>
					</input><span>Wrong feeds page? </span>
					<input id="reset_button" value="Reset page!" type="submit" />
				</form>
			</xsl:if>
		</div>
	
	</xsl:template>
	
	<!-- CREATE PAGE -->
	
	<!-- create page structure -->
	<xsl:template match="root">
		<html>
			<!-- head -->
			<head>
				<!-- load common code -->
				<xsl:call-template name="Loader">
					<xsl:with-param name="src" select="$src_head"/>
				</xsl:call-template>
				<!-- load extra head params -->
				<xsl:call-template name="ExtraDataLoader">
					<xsl:with-param name="src" select="extradata/head"/>
				</xsl:call-template>
				<!-- load ie fix -->
				<xsl:call-template name="IELoader">
					<xsl:with-param name="src" select="$src_head_ie/root/common"/>
				</xsl:call-template>
				<xsl:call-template name="IELoader">
					<xsl:with-param name="src" select="extradata/IEFix"/>
				</xsl:call-template>
				<!-- pagetitle -->
				<title><xsl:value-of select="pagetitle"/></title>
			</head>
			<body>
				<!-- body onload attribute -->
				<xsl:if test="extradata/body/onload/text() != ''">
					<xsl:attribute name="onLoad"><xsl:value-of select="extradata/body/onload"/></xsl:attribute>
				</xsl:if>
				<!-- header -->			
				<xsl:call-template name="Loader">
					<xsl:with-param name="src" select="$src_header"/>
				</xsl:call-template>
				<!-- navbar -->
				<!-- if signup page -->
				<xsl:if test="page = 'signup'">
					<div id="navbar">
						<xsl:call-template name="NavLoader">
							<xsl:with-param name="src" select="$src_navbar"/>
						</xsl:call-template>
					</div>
				</xsl:if>
				<!-- if feedscribe page -->
				<xsl:if test="page = 'feedscribe'">
					<div id="main_app_navigation">
						<xsl:call-template name="AppNavLoader">
							<xsl:with-param name="src" select="$src_navbar"/>
						</xsl:call-template>
					</div>
				</xsl:if>
				<!-- content -->
				<xsl:call-template name="FeedscribeContent">
					<!--<xsl:with-param name="src" select="$src_footer"/>-->
				</xsl:call-template>				
				<!-- footer -->
				<xsl:call-template name="Loader">
					<xsl:with-param name="src" select="$src_footer"/>
				</xsl:call-template>				
			</body>
		</html>
	</xsl:template>
	
</xsl:stylesheet>	
